<?php
//默认前台控制器
class passportController extends webpageController{
    protected $layout = "passport/admin/layout";
    public function __construct(){
        parent::__construct();
        $this->UserInfo = model('user')->islogin(); //读取登录信息!
        if(!$this->UserInfo){
            $this->redirect(url('default/user/index'));
            return FALSE;
        }
        $this->assign('LoginInfo',$this->UserInfo); 
    }
    //网站首页
    //###########################################
    public function index(){
        $this->display('passport/admin/index');
    }
    //修改密码
    //###########################################
    public function password(){
        if($this->isPost()){
            $password = trim($_POST['password']);
            $newpassword = trim($_POST['newpassword']);
            $checkpassword = trim($_POST['checkpassword']);
            $msg = Check::rule(
                    array(Check::must($password), '请输入现在的密码') ,
                    array(Check::must($newpassword), '请输入新密码'),
                    array(Check::must($checkpassword), '请输入新密码'),
                    array(check::same($newpassword,$checkpassword),'两次新密码输入不相同'));
            if(true !== $msg){
                $this->jsonMsg($msg);
            }
            $userInfo = $this->UserInfo;
            $password = md5($password);
            if($userInfo['password'] == $password){
                $data['password'] = md5($newpassword);
                $condition['id'] = $UserInfo['id'];
                model('passport')->update($condition,$data);
                $this->jsonMsg('修改成功',1,url('user/logout',array('callback'=>url('user/index'))));
            }else{
                $this->jsonMsg('修改失败,原始密码错误.');
            }
        }else{
            $this->display('passport/admin/password');
        }
    } 
    //修改手机
    //###########################################
    public function iphone(){
        if($this->isPost()){
            $msg = Check::rule(
                array(check::must($_POST['iphone']), '手机号没有填写'),
                array(check::mobile($_POST['iphone']), '手机号式输入错误'),
                array(check::must($_POST['password']), '密码没有填写')
            );
            if ($msg !== true) {
                $this->jsonMsg($msg);
            }
            $password = md5($_POST['password']);
            $userInfo = $this->UserInfo;
            if($userInfo['safepassword'] == $password){
                $data['iphone'] = $_POST['iphone'];
                $rel = model('passport')->update(array('id'=>$userInfo['id']),$data);
                if(!$rel){
                    $this->jsonMsg('修改失败');
                }
                $this->jsonMsg('修改成功',1,url('passport/iphone'));
            }else{
                $this->jsonMsg('安全密码不正确');
            }
        }else{
            $this->display('passport/admin/iphone');
        }
    }
    //修改信箱
    //###########################################
    public function email(){
        if($this->isPost()){
            $msg = Check::rule(
                array(check::must($_POST['email']), '邮箱没有填写'),
                array(check::email($_POST['email']), '邮箱式输入错误'),
                array(check::must($_POST['password']), '密码没有填写')
            );
            if ($msg !== true) {
                $this->jsonMsg($msg);
            }
            $password = md5($_POST['password']);
            $userInfo= $this->UserInfo;
            if($userInfo['safepassword'] == $password){
                $data['email'] = $_POST['email'];
                $rel = model('passport')->update(array('id'=>$userInfo['id']),$data);
                if(!$rel){
                    $this->jsonMsg('修改失败');
                }
                $this->jsonMsg('修改成功',1,url('passport/iphone'));
            }else{
                $this->jsonMsg('安全密码不正确');
            }
        }else{
            $this->display('passport/admin/email');
        }
    } 
    //修改资料
    public function info(){
        $this->display('passport/admin/info');
    } 
    //修改头像
    public function photo(){
        $this->display('passport/admin/photo');
    }  
    //第三方登录
    public function openid(){
        $this->display('passport/admin/openid');
    }  
}
